package cn.com.yang.config;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

/**
 * 线程池控制类
 * 
 * @author yangyijin
 * @since 2024年7月23日-上午9:36:33
 */
@Configuration
public class ThreadPoolConfig {
	@Value("${rocketmq.pool.fixed.max.num:100}")
	private int mqFixedSize;
	
	@Value("${rocketmq.pool.cached.max.num:100}")
	private int mqCachedSize;
	
	@Value("${rocketmq.pool.fixed.timeout:30}")
	private int mqFixedTimeout;

	/**
	 * MQ调用者请求者线程池
	 */
	@Value("${rocketmq.pool.define.core.num:20}")
	private int mqDefineCoreSize;
	
	@Value("${rocketmq.pool.define.max.num:100}")
	private int mqDefineMaxSize;

	@Value("${rocketmq.pool.define.queue.num:50}")
	private int mqDefineQueueSize;

	@Value("${rocketmq.pool.define.keepalive:3}")
	private int mqDefineKeepAlive;
	
	/***
	 * MQ消费者线程池
	 */
	@Value("${rocketmq.pool.consume.core.num:20}")
	private int mqConsumeCoreSize;
	
	@Value("${rocketmq.pool.consume.max.num:100}")
	private int mqConsumeMaxSize;

	@Value("${rocketmq.pool.consume.queue.num:50}")
	private int mqConsumeQueueSize;

	@Value("${rocketmq.pool.consume.keepalive:3}")
	private int mqConsumeKeepAlive;
	
	/***
	 * MQ生产者线程池
	 */
	@Value("${rocketmq.pool.producer.core.num:50}")
	private int mqProducerCoreSize;
	
	@Value("${rocketmq.pool.producer.max.num:200}")
	private int mqProducerMaxSize;

	@Value("${rocketmq.pool.producer.queue.num:50}")
	private int mqProducerQueueSize;

	@Value("${rocketmq.pool.producer.keepalive:3}")
	private int mqProducerKeepAlive;

	/**
	 * 固定线程池
	 * 
	 * @return
	 */
	@Bean("fixedMQthreadPool")
	public ExecutorService fixedMQthreadPool() {
		return Executors.newFixedThreadPool(mqFixedSize);
	}

	/**
	 * 缓存线程池
	 * 
	 * @return
	 */
	@Bean("cachedMQthreadPool")
	public ExecutorService cachedMQthreadPool() {
		return Executors.newFixedThreadPool(mqCachedSize);
	}
	

	/**
	 * 自定义MQ线程池
	 * 
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Bean("rocketMQExecutor")
	public ThreadPoolExecutor rocketMQExecutor() {
		BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue(mqDefineQueueSize);
		ThreadFactory nameFactory = new ThreadFactoryBuilder().setNameFormat("mq-pool-%d").build();
		RejectedExecutionHandler hander = new ThreadPoolExecutor.AbortPolicy();
		return new ThreadPoolExecutor(mqDefineCoreSize, mqDefineMaxSize, mqDefineKeepAlive, TimeUnit.SECONDS, workQueue,
				nameFactory, hander);
	}
	
	
	/**
	 * 自定义MQ消费者线程池
	 * 
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Bean("rocketMQConsumeExecutor")
	public ThreadPoolExecutor rocketMQConsumeExecutor() {
		BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue(mqConsumeQueueSize);
		ThreadFactory nameFactory = new ThreadFactoryBuilder().setNameFormat("mq-consume-pool-%d").build();
		RejectedExecutionHandler hander = new ThreadPoolExecutor.AbortPolicy();
		return new ThreadPoolExecutor(mqConsumeCoreSize, mqConsumeMaxSize, mqConsumeKeepAlive, TimeUnit.SECONDS, workQueue,
				nameFactory, hander);
	}
	/**
	 * MQ生产者线程池
	 * @return
	 */
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Bean("rocketMQProducerExecutor")
	public ThreadPoolExecutor rocketMQProducerExecutor() {
		BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue(mqProducerQueueSize);
		ThreadFactory nameFactory = new ThreadFactoryBuilder().setNameFormat("mq-producer-pool-%d").build();
		RejectedExecutionHandler hander = new ThreadPoolExecutor.AbortPolicy();
		return new ThreadPoolExecutor(mqProducerCoreSize, mqProducerMaxSize, mqProducerKeepAlive, TimeUnit.SECONDS, workQueue,
				nameFactory, hander);
	}
}
